package com.example.common.base

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.example.common.tools.LogUtils

abstract class BaseApplication : Application() {


    var mActivityCount = 0

    override fun onCreate() {
        super.onCreate()
        registerActLifecycleCallback()
    }

    /**
     * app 前后台切换监听
     */
    private fun registerActLifecycleCallback() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, bundle: Bundle?) {

            }

            override fun onActivityStarted(activity: Activity) {
                mActivityCount++
                if (mActivityCount == 1) {
                    LogUtils.e("MyApp", "后台->前台")
                    onReception()
                }
            }

            override fun onActivityResumed(p0: Activity) {
            }

            override fun onActivityPaused(p0: Activity) {
            }


            override fun onActivityStopped(activity: Activity) {
                mActivityCount--
                if (mActivityCount == 0) {
                    LogUtils.e("MyApp", "前台->后台")
                    onBackstage()
                }
            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
            }

            override fun onActivityDestroyed(p0: Activity) {
            }

        })
    }

    abstract fun onBackstage()
    abstract fun onReception()

}

